using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

using EasySeries.Metier.Entites;

namespace EasySeries.LibXMLArea.GeneralParsingTools
{
    public class Xml_Parser
    {

        public static IList<Serie> getSeries(BackgroundWorker worker, DoWorkEventArgs e)
        {
            IList<Serie> list_series = new List<Serie>();
            XmlDocument Xd = new XmlDocument();

            try
            {
                String url = Program.ContexteParameters["Files"]["series"];

                Xd.Load(url);
                // Xd.Load("C:/Projets/Dot net/TVSeries/TVSeries/EasySeries/bin/Debug/series.xml");
            }
            catch (Exception ex)
            {
                e.Cancel = true;
                return null;
            }

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return null;
            }

            foreach (XmlNode xn in Xd.DocumentElement.ChildNodes)
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    return null;
                }

                Serie s = new Serie();
                s.Name = xn.InnerText;
                list_series.Add(s);
            }
            return list_series;    
        }

	    public static void ParseRss()
        {
            IList<string> res = new List<string>();
            XmlDocument Xd = new XmlDocument();
            Xd.Load("Rss.xml");
            //IList<Abonnement> List_Ab = new List<Abonnement>();
            foreach (XmlNode Child in Xd.GetElementsByTagName("item"))
            {
                foreach (XmlNode C_Child in Child.ChildNodes)
                {
                    if (C_Child.Name == "title")
                        res.Add(C_Child.InnerText);
                }
            }
        }

        public static void updateEpisodesSerie(ref Serie serie)
        {
            IList<Episode> list_episodes = new List<Episode>();
            XmlDocument Xd = new XmlDocument();

             String url = "http://tvrss.net/search/index.php?distribution_group=vtv&show_name=" + serie.Name.Replace(" ", "+") + "&mode=rss";
            //String url = "C:/Projets/Dot net/TVSeries/TVSeries/EasySeries/bin/Debug/" + serie.Name.Replace(" ", "+") + ".xml";
            
            try
            {
                Xd.Load(url);
            }
            catch (Exception e)
            {
                return;
            }

            foreach (XmlNode Child in Xd.GetElementsByTagName("item"))
            {
                foreach (XmlNode C_Child in Child.ChildNodes)
                {
                    if (C_Child.Name == "title")
                    {
                        Episode e = new Episode();
                        Regex regEmail = new Regex(@"([0-9]{1,2})x([0-9]{1,2})");
                        Match monMatch = regEmail.Match(C_Child.InnerText);
                        if (monMatch.Success)
                        {
                            e.NumSeason = Int32.Parse(monMatch.Groups[1].Value);
                            e.NumEpisode = Int32.Parse(monMatch.Groups[2].Value);
                        }
                        list_episodes.Add(e);
                    }
                }
                serie.Episodes = list_episodes;
            }
            
        }
    }
}
